import React, { Component } from 'react';
import { Redirect, Switch, Route } from 'react-router-dom';

import BasicLayout from '../layouts/BasicLayout/BasicLayout';
import NotFound from '../common/NotFound.jsx';
import { asideMenuConfig } from '../menuConfig';
import { routerData } from './routerList';
// import LoginPanel from '../pages/login/loginPanel';
//import { checkAuthState } from '../utils/checkStore';

class MainRoutes extends Component {
  static displayName = 'MainRoutes';

  /**
   * 根据菜单取得重定向地址.
   */
  getRedirectData = () => {
    const redirectData = [];
    const getRedirect = (item) => {
      if (item && item.children) {
        if (item.children[0] && item.children[0].path) {
          redirectData.push({
            from: `${item.path}`,
            to: `${item.children[0].path}`,
          });
          item.children.forEach((children) => {
            getRedirect(children);
          });
        }
      }
    };

    asideMenuConfig.forEach(getRedirect);

    return redirectData;
  };

  /**
   * 渲染路由组件
   */
  renderNormalRoute = (item, index) => {
    // 如果管理员已经登录
    // console.log(checkAuthState().isAuth);
    //if (checkAuthState().isAuth) {
      // 登录过
      // return item.component ? (
      //   <Route
      //     key={index}
      //     path={item.path}
      //     render={
      //       () => (<BasicLayout contentCp={item.component} defaultKeys={item.path} />)
      //     }
      //     exact={item.exact}
      //   />
      // ) : null;

      // if(item.layout==='mainLayout'){
      //   //sessionStorage.clear();
      //   return <Route key={index} path="/login" component={LoginPanel} exact={true}/>
      // }
      // else{
        return item.component&&item.layout!=='mainLayout' ? (
          <Route
            key={index}
            path={item.path}
            render={
              () => (<BasicLayout contentCp={item.component} defaultKeys={item.path} />)
            }
            exact={item.exact}
          />
        ) : null;
      // }
    //}
    // else{
    //   return <Route key={index} path="/" component={LoginPanel} />;
    // }
  };

  render() {
    const redirectData = this.getRedirectData();
    return (
      <Switch>
        {/* 渲染权限路由表 */}

        {routerData.map(this.renderNormalRoute)}
        {/* 路由重定向，嵌套路由默认重定向到当前菜单的第一个路由 */}
        {redirectData.map((item, index) => {
          return <Redirect key={index} exact from={item.from} to={item.to} />;
        })}

        {/* 首页默认重定向到 /login */}
        <Redirect exact from="/" to="/login" />

        {/* 未匹配到的路由重定向到 404 */}
        <Route component={NotFound} />
      </Switch>
    );
  }
}

export default MainRoutes;

